﻿using System.Windows;
using System.Windows.Media;

namespace ImageUploaderComponent {
    public class TreeHelper {
        /// <summary>
        /// Find a parent element in the visual tree of type T with x:Name = name
        /// </summary>
        /// <typeparam name="T">Type of element to find</typeparam>
        /// <param name="name">Name of element to find</param>
        /// <param name="startingPoint">Who are we starting with?</param>
        /// <returns></returns>
        public T FindParentWithName<T>(string name, FrameworkElement startingPoint ) where T:FrameworkElement {
            var parent = startingPoint.Parent;
            if(parent == null){
                return null;
            }
            if(parent is T && ((T)parent).Name==name){
                return (T)parent;
            }
            if(parent is FrameworkElement){
                return FindParentWithName<T>(name, (FrameworkElement) parent);
            }
            return null;
        }

        public Point GetPositionOfRenderedElement(UIElement display){
            GeneralTransform transform = display.TransformToVisual(Application.Current.RootVisual);
            Point offset = transform.Transform(new Point(0, 0));
            return offset;
        }
    }
}
